package com.springboot2.service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.springboot2.entity.Menu;
import com.springboot2.entity.Role;
import com.springboot2.entity.RoleMenu;
import com.springboot2.entity.RoleResource;
import com.springboot2.mapper.RoleMapper;
import com.springboot2.mapper.RoleMenuMapper;
import com.springboot2.mapper.RoleResourceMapper;
import com.springboot2.service.MenuService;
import com.springboot2.service.ResourceService;
import com.springboot2.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private MenuService menuService;

    @Resource
    private RoleResourceMapper roleResourceMapper;

    @Resource
    private ResourceService resourceService;

    @Override
    public void add(Role role) {
        roleMapper.add(role);
    }

    @Override
    public void del(int id) {
        roleMapper.del(id);
    }

    @Override
    public void update(Role role) {
        roleMapper.update(role);
    }

    @Override
    public Role findById(int id) {
        return roleMapper.findById(id);
    }

    @Override
    public List<Role> findByPage(int pageNo, int pageSize) {
        Map<String ,Integer> map = new HashMap<>();
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        return roleMapper.findByPage(map);
    }

    @Override
    public int count() {
        return roleMapper.count();
    }

    @Override
    public List<Role> findByPageParams(int pageNo, int pageSize, String name) {
        Map<String,Object> map = new HashMap<>();
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("name",name+"%");
        return roleMapper.findByPageParams(map);
    }

    @Override
    public int countParams() {
        return roleMapper.countParams();
    }

    @Transactional
    @Override
    public void setRoleMenu(Integer roleId, List<Integer> menuIds) {
        //先删除当前端角色id所有的菜单绑定关系
        roleMenuMapper.del(roleId);
        //再把前端传过来的菜单id绑定到当前角色Id上

        List<Integer> menuIdsCopy = CollUtil.newArrayList(menuIds);
        for (Integer menuId:menuIds){
            Menu menu = menuService.findById(menuId);
            if(menu.getPid() != null && !menuIdsCopy.contains(menu.getPid())){
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menu.getPid());
                roleMenuMapper.add(roleMenu);
                menuIdsCopy.add(menu.getPid());
            }
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuMapper.add(roleMenu);

        }

    }

    @Override
    public List<Integer> findByRoleMenu(Integer roleId) {

        return roleMenuMapper.findByRoleMenu(roleId);
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }

    @Override
    @Transactional
    public void setRoleResource(Integer roleId, List<Integer> resourceIds) {

        // 先删除
        roleResourceMapper.del(roleId);
        // 再添加
//        for (Integer resourceId:resourceIds){
//            RoleResource roleResource = new RoleResource();
//            roleResource.setRoleId(roleId);
//            roleResource.setResourceId(resourceId);
//            roleResourceMapper.add(roleResource);
//        }

        List<Integer> resourceIdsCopy = CollUtil.newArrayList(resourceIds);
        for (Integer resourceId:resourceIds){
            com.springboot2.entity.Resource resource = resourceService.findById(resourceId);
            if(resource.getPid() != null && !resourceIdsCopy.contains(resource.getPid())){
                RoleResource roleResource = new RoleResource();
                roleResource.setRoleId(roleId);
                roleResource.setResourceId(resource.getPid());
                roleResourceMapper.add(roleResource);
                resourceIdsCopy.add(resource.getPid());
            }
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(resourceId);
            roleResourceMapper.add(roleResource);

        }





//        List<Integer> resourceIdsCopy = CollUtil.newArrayList(resourceIds);
//        for (Integer resourceId:resourceIds) {
//            com.springboot2.entity.Resource resource = resourceService.findById(resourceId);
////            if(resource.getPid() != null && !resourceIdsCopy.contains(resource.getPid())){
////                RoleResource roleResource = new RoleResource();
////                roleResource.setRoleId(roleId);
////                roleResource.setResourceId(resourceId);
////                roleResourceMapper.add(roleResource);
////                resourceIdsCopy.add(resource.getPid());
////            }
//
//            RoleResource roleResource = new RoleResource();
//            roleResource.setRoleId(roleId);
//            roleResource.setResourceId(resourceId);
//            roleResourceMapper.add(roleResource);
//
//        }


    }

    @Override
    public List<Integer> findByRoleResource(Integer roleId) {
        return roleResourceMapper.findByRoleId(roleId);
    }

}
